1. Project Clover database Tue Apr 9 2024 18:10:53 CDT
  2. Package com.alibaba.fastjson.util

File IdentityHashMap.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

18
35
7
2
112
72
16
0.46
5
3.5
2.29

Classes

Class Line # Actions
IdentityHashMap 26 31 0% 15 0
1.0100%
IdentityHashMap.Entry 93 4 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 3687 tests. .

Source view

1    /*
2    * Copyright 1999-2017 Alibaba Group.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package com.alibaba.fastjson.util;
17   
18    import java.util.Arrays;
19   
20    /**
21    * for concurrent IdentityHashMap
22    *
23    * @author wenshao[szujobs@hotmail.com]
24    */
25    @SuppressWarnings("unchecked")
 
26    public class IdentityHashMap<K, V> {
27    private final Entry<K, V>[] buckets;
28    private final int indexMask;
29    public final static int DEFAULT_SIZE = 8192;
30   
 
31  79 toggle public IdentityHashMap(){
32  79 this(DEFAULT_SIZE);
33    }
34   
 
35  574 toggle public IdentityHashMap(int tableSize){
36  574 this.indexMask = tableSize - 1;
37  574 this.buckets = new Entry[tableSize];
38    }
39   
 
40  58181813 toggle public final V get(K key) {
41  58181813 final int hash = System.identityHashCode(key);
42  58181814 final int bucket = hash & indexMask;
43   
44  58182970 for (Entry<K, V> entry = buckets[bucket]; entry != null; entry = entry.next) {
45  58171997 if (key == entry.key) {
46  58170842 return (V) entry.value;
47    }
48    }
49   
50  10972 return null;
51    }
52   
 
53  126 toggle public Class findClass(String keyString) {
54  941501 for (int i = 0; i < buckets.length; i++) {
55  941394 Entry bucket = buckets[i];
56   
57  941394 if (bucket == null) {
58  875510 continue;
59    }
60   
61  135549 for (Entry<K, V> entry = bucket; entry != null; entry = entry.next) {
62  69684 Object key = bucket.key;
63  69684 if (key instanceof Class) {
64  66644 Class clazz = ((Class) key);
65  66644 String className = clazz.getName();
66  66644 if (className.equals(keyString)) {
67  19 return clazz;
68    }
69    }
70    }
71    }
72   
73  107 return null;
74    }
75   
 
76  18605 toggle public boolean put(K key, V value) {
77  18605 final int hash = System.identityHashCode(key);
78  18605 final int bucket = hash & indexMask;
79   
80  18939 for (Entry<K, V> entry = buckets[bucket]; entry != null; entry = entry.next) {
81  361 if (key == entry.key) {
82  27 entry.value = value;
83  27 return true;
84    }
85    }
86   
87  18578 Entry<K, V> entry = new Entry<K, V>(key, value, hash, buckets[bucket]);
88  18578 buckets[bucket] = entry; // 并发是处理时会可能导致缓存丢失,但不影响正确性
89   
90  18578 return false;
91    }
92   
 
93    protected static final class Entry<K, V> {
94   
95    public final int hashCode;
96    public final K key;
97    public V value;
98   
99    public final Entry<K, V> next;
100   
 
101  18578 toggle public Entry(K key, V value, int hash, Entry<K, V> next){
102  18578 this.key = key;
103  18578 this.value = value;
104  18578 this.next = next;
105  18578 this.hashCode = hash;
106    }
107    }
108   
 
109  2 toggle public void clear() {
110  2 Arrays.fill(this.buckets, null);
111    }
112    }